<?php
class WorksAction extends CommonAction {
	public function data() {
		$array = array();
		$method = $_POST;
		$methodget = $_GET;
		$workdayObj = D('workday');
		$userObj = new UserModel();
		switch($method['do']){
			case 'id':
				$map['uid'] = $method['where'];
				$map['date'] = strtotime($method['date']);
				$workday = $workdayObj->where($map)->find();
				$workday['date'] = date('Y-m-d',$workday['date']);
				echo json_encode($workday);
			break;
			default:
				$d = getcity();
				$userinfo = Session::get('userinfo');
				switch($userinfo['type_id']){
					case 1:
						$array = $userObj->field('id,account,nickname')->select();
						foreach($array as $v)
						{
							$ids[] = $v[id];
						}
						$mapworks['uid'] = array('in',$ids);
						$mapworks['date'] = strtotime($method['date']);
						foreach($workdayObj->where($mapworks)->select() as $v){
							$workday[$v['uid']] = $v;
						}
					break;
					case 2:
						$usermap['did'] = $userinfo['did'];
						$array = $userObj->field('id,account,nickname')->where($usermap)->select();
						foreach($array as $v)
						{
							$ids[] = $v[id];
						}
						$mapworks['uid'] = array('in',$ids);
						$mapworks['date'] = strtotime($method['date']);
						foreach($workdayObj->where($mapworks)->select() as $v){
							$workday[$v['uid']] = $v;
						}
					break;
				}
				//print_r($workday);
				foreach($array as $v)
				{
					unset($v['password']);
					$v['date'] = date('Y-m-d',$workday[$v['id']]['date']);
					$v['intro'] = $workday[$v['id']]['intro'];
					$v['type'] = $workday[$v['id']]['type'];
					$workdays['list'][] = $v;
				}
				echo json_encode($workdays);
			break;
		}
	}
	public function index() {
		$this->display();
	}

	public function edit() {
		if($this->isPost())
		{
			/*
			['id','type','date','intro']
			*/
			$method = $_POST;
			$data = array();
			$userObj = new UserModel();
			$workObj = new WorkModel();
			$mapdate['date'] = strtotime($method['dates']);
			$mapdate['uid'] = $method['uid'];
			if($work = $workObj->where($mapdate)->find())
			{
				$workObj->id = $work['id'];
				$workObj->type = $method['type'];
				$workObj->intro = $method['intro'];
				//$workObj->date = strtotime($method['dates']);
				if($workObj->save()){
					$this->success('修改成功');
				}else{
					$this->error('修改失败');
				}
			}else{
				$data['uid'] = $method['uid'];
				$data['type'] = $method['type'];
				$data['intro'] = $method['intro'];
				$data['date'] = strtotime($method['dates']);
				if($workObj->add($data)){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}

			}

		}
	}
}